package cn.javablog.controller;


import cn.javablog.Vo.CommentVo;
import cn.javablog.Vo.PageVo;
import cn.javablog.common.lang.Result;
import cn.javablog.entity.TComment;
import cn.javablog.entity.TUser;
import cn.javablog.service.TCommentService;
import cn.javablog.service.TUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author AllenYe
 * @since 2020-08-31
 */
@RestController
@RequestMapping("/comments")
public class TCommentController {
    @Autowired
    private TCommentService tCommentService;
    @Autowired
    private TUserService tUser;
    /**
     * 分页查询评论
     * @param pageVo
     * @return
     */
    @RequiresAuthentication
    @RequestMapping("/list")
    public Result listComment(@RequestBody PageVo pageVo){
        Page page = new Page(pageVo.getPageCurrent() == 0 ? 1 : pageVo.getPageCurrent(),pageVo.getPageSize() == 0 ? 10 : pageVo.getPageSize());
        QueryWrapper<TComment> wrapper = new QueryWrapper<>();
        wrapper.like("nickname",pageVo.getName());
        IPage page1 = tCommentService.page(page, wrapper);
        return Result.succ(page1);
    }
    /**
     * 添加评论
     * @param commentVo
     * @return
     */
    @RequiresAuthentication
    @RequestMapping("/add")
    public Result addComment(@Validated @RequestBody CommentVo commentVo){
        TUser tuserObj = tUser.getOne(new QueryWrapper<TUser>().eq("nickname", commentVo.getNickname()));
        Assert.notNull(tuserObj,"该用户不存在");
        TComment comment = new TComment();
        comment.setNickname(commentVo.getNickname());
        comment.setAvatar(commentVo.getAvatar());
        comment.setCreateTime(new Date());
        comment.setState(Long.valueOf(1));
        comment.setParentCommentId(Long.valueOf(-1));
        comment.setContent(commentVo.getContent());
        comment.setEmail(commentVo.getEmail());
        boolean b = tCommentService.save(comment);
        if(b){
            return Result.succ("评论成功,等待博主通过",null);
        }else{
            return Result.fail("评论失败");
        }
    }
    /**
     * 审批评论
     * @param commentVo
     * @return
     */
    @RequiresAuthentication
    @RequestMapping("/edit")
    public Result editComment(@Validated @RequestBody CommentVo commentVo){
        TUser tuserObj = tUser.getOne(new QueryWrapper<TUser>().eq("nickname", commentVo.getNickname()));
        Assert.notNull(tuserObj,"该用户不存在");
        TComment comment = tCommentService.getOne(new QueryWrapper<TComment>().eq("id", commentVo.getId()));
        comment.setContent(commentVo.getContent());
        comment.setEmail(commentVo.getEmail());
        comment.setState(Long.valueOf(commentVo.getState()));
        boolean b = tCommentService.update(comment, new QueryWrapper<TComment>().eq("id", commentVo.getId()));
        if(b){
            return Result.succ("审批成功",null);
        }else{
            return Result.fail("审批失败");
        }
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @RequiresAuthentication
    @RequestMapping("/del/{id}")
    public Result delComment(@PathVariable int id){
        TComment comment = tCommentService.getOne(new QueryWrapper<TComment>().eq("id", id));
        Assert.notNull(comment,"该评论不存在");
        boolean b = tCommentService.removeById(id);
        if(b){
            return Result.succ("删除成功",null);
        }else{
            return Result.fail("删除失败");
        }
    }
}
